package com.unitedvision.persistence.domain;

import javax.persistence.Column;
import javax.persistence.Embeddable;

import com.unitedvision.core.domain.Contact;
import com.unitedvision.core.exception.WrongFormatException;

@Embeddable
public class ContactValue {
	private String email;
	private String phone;
	
	public ContactValue() {
		super();
	}

	public ContactValue(String email, String phone) throws WrongFormatException {
		super();
		setEmail(email);
		setPhone(phone);
	}
	
	public ContactValue(Contact contact) throws WrongFormatException {
		super();
		setEmail(contact.getEmail());
		setPhone(contact.getPhone());
	}

	@Column (name = "email")
	public String getEmail() {
		return email;
	}

	public void setEmail(String email) throws WrongFormatException {
		checkEmailFormat(email);
		this.email = email;
	}

	@Column (name = "phone")
	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) throws WrongFormatException {
		checkPhoneFormat(phone);
		this.phone = phone;
	}
	
	public void checkEmailFormat(String email) throws WrongFormatException {
		//TODO not implemented
	}
	
	public void checkPhoneFormat(String phone) throws WrongFormatException {
		//TODO not implemented
	}
	
	public Contact toContact() {
		return new Contact(email, phone);
	}
	
	public static ContactValue create(String email, String phone) throws WrongFormatException {
		return new ContactValue(email, phone);
	}
	
	public static ContactValue create(Contact contact) throws WrongFormatException {
		return new ContactValue(contact);
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((email == null) ? 0 : email.hashCode());
		result = prime * result + ((phone == null) ? 0 : phone.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		
		ContactValue other = (ContactValue) obj;
		
		if (email == null) {
			if (other.email != null)
				return false;
		} else if (!email.equals(other.email))
			return false;
		
		if (phone == null) {
			if (other.phone != null)
				return false;
		} else if (!phone.equals(other.phone))
			return false;
		
		return true;
	}

	@Override
	public String toString() {
		return "ContactValue [email=" + email + ", phone=" + phone + "]";
	}
}
